
/**
 * type: goodsObj 购买物品 itemNo 购买物品id stockNo 购买物品规格
 * data: 扩展数据对象，用于保存参数
 */
let app = getApp();
function setGoods(goodsObj, itemNo, stockNo, shopNo) {
  wx.getStorage({
    key: 'shopCarInfo',
    success: function (res) {

      let resArr = res.data;
      new Promise((reslove,reject) => {
        let setArr = [];
        for (let arrItem of resArr) {
          if (arrItem.shopNo == shopNo) {
            setArr = arrItem;
          }
        }
        reslove(setArr)
      }).then((res) => {
        // 同店铺
        let setArr = res;
        // 同商品
        let yigood = false;
        if (setArr && setArr.list.length > 0){
          for (let resItem of setArr.list) {
            if (resItem.stockNo == stockNo) {
              resItem.buyNumber = resItem.buyNumber + goodsObj.buyNumber;
              resItem.buyMoney = parseFloat(resItem.price) * (resItem.buyNumber);
              yigood = true;
            }
          }
          if (!yigood) {
            setArr.list.push(goodsObj)
          }
        }else{
          let resArrObj = {
            shopNo: shopNo,
            shopName: goodsObj.shopName,
            checked: false,
            list: []
          };
          goodsObj.buyMoney = parseFloat(goodsObj.price) * goodsObj.buyNumber;
          resArrObj.list.push(goodsObj)
          resArr.push(resArrObj);
        }
        
        let no = 0;
        for (let listItem of resArr) {
          no += listItem.list.length;
        }
        app.globalData.goodsNo = no;
        console.log(app.globalData)
        wx.setStorage({
          key: 'shopCarInfo',
          data: resArr
        })
      })

      
      
      // 不同店铺,商品，类型
      // let yigood = false;
      // for (let resItem of resArr){
      //   if (resItem.itemNo == itemNo){
      //     for (let item of resItem.itemSkuResultDtos) {
      //       if (item.stockNo == stockNo && item.checked == true) {
      //         resItem.buyNumber = resItem.buyNumber + goodsObj.buyNumber;
      //         yigood = true;
      //       }
      //     }
      //   }
      // }
      // if (!yigood){
      //   resArr.push(goodsObj)
      // }
      // wx.setStorage({
      //   key: 'shopCarInfo',
      //   data: resArr
      // })
    },
    fail() {
      // let resArr = [];
      let resArr = [{
        shopNo: shopNo,
        shopName: goodsObj.shopName,
        checked:false,
        list:[]
      }]
      // console.log(goodsObj)
      goodsObj.buyMoney = parseFloat(goodsObj.price) *  goodsObj.buyNumber;
      resArr[0].list.push(goodsObj);
      
      let no = 0;
      for (let listItem of resArr) {
        no += listItem.list.length;
      }
      app.globalData.goodsNo = no;
      console.log(app.globalData)
      wx.setStorage({
        key: 'shopCarInfo',
        data: resArr
      })
    }
  })
}

// 用于处理详情，购物车立即购买参数数据处理，购买商品数量和价格合计
function setBuyNowGoods(resArr){
  console.log(resArr)
  // 把没被选的商品剔除
  for (let [ind, arrItem] of resArr.entries()){
    for (let [index, listItem] of arrItem.list.entries()){
      if (!listItem.checked){
        arrItem.list.splice(index, 1);
        if (arrItem.list.length == 0) {
          resArr.splice(ind, 1);
        }
      }
    }
  }
  console.log(resArr)
  

  wx.setStorage({
    key: 'buyNowInfo',
    data: resArr
  })

}



module.exports = {
  setGoods: setGoods,
  setBuyNowGoods: setBuyNowGoods
}